<template>
  <el-form
    :model="dataForm"
    :rules="dataRule"
    ref="dataForm"
    @keyup.enter.native="dataFormSubmit()"
    label-width="120px"
  >
    <el-form-item
      label="发票类型"
      prop="type"
    >
      <el-radio-group
        v-model="dataForm.type"
        @change="typeChange"
      >
        <el-radio :label="'1'">公司普通发票</el-radio>
        <el-radio :label="'2'">公司专用发票</el-radio>
        <el-radio :label="'3'">个人普通发票</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item
      label="发票号码"
      prop="number"
    >
      <el-input
        v-model="dataForm.number"
        placeholder="必填"
      ></el-input>
    </el-form-item>
    <el-form-item
      label="发票金额"
      prop="price"
    >
      <el-input
        v-model="dataForm.price"
        placeholder="必填"
      ></el-input>
    </el-form-item>
    <el-form-item
      label="发票代码"
      prop="code"
    >
      <el-input
        v-model="dataForm.code"
        placeholder=""
      ></el-input>
    </el-form-item>
    <el-form-item
      label="发票税额"
      prop="taxAmount"
    >
      <el-input
        v-model="dataForm.taxAmount"
        placeholder=""
      ></el-input>
    </el-form-item>
    <el-form-item
      label="发票抬头"
      prop="title"
      :rules="{
        required: this.typeFlag, message: '发票抬头不能为空', trigger: 'blur'
      }"
    >
      <el-input
        v-model="dataForm.title"
        :placeholder="this.typeFlag?'必填':''"
      ></el-input>
    </el-form-item>
    <el-form-item
      label="地址电话"
      prop="addressPhone"
      :rules="{
        required: this.typeFlag, message: '地址电话不能为空', trigger: 'blur'
      }"
    >
      <el-input
        v-model="dataForm.addressPhone"
        :placeholder="this.typeFlag?'必填':''"
      ></el-input>
    </el-form-item>
    <el-form-item
      label="纳税人识别号"
      prop="idNum"
      :rules="{
        required: this.typeFlag, message: '纳税人识别号不能为空', trigger: 'blur'
      }"
    >
      <el-input
        v-model="dataForm.idNum"
        :placeholder="this.typeFlag?'必填':''"
      ></el-input>
    </el-form-item>
    <el-form-item
      label="开户行及账号"
      prop="bankAccount"
      :rules="{
        required: this.typeFlag, message: '开户行及账号不能为空', trigger: 'blur'
      }"
    >
      <el-input
        v-model="dataForm.bankAccount"
        :placeholder="this.typeFlag?'必填':''"
      ></el-input>
    </el-form-item>
    <el-form-item
      label="入账人"
      prop="userId"
    >
      <el-select
        v-model="dataForm.userId"
        placeholder="请选择"
      >
        <el-option
          :label="this.info.peopleEntity.name"
          :value="this.info.peopleEntity.id"
        >
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item
      label="备注"
      prop="note"
    >
      <el-input
        type="textarea"
        v-model="dataForm.note"
        placeholder="备注"
      ></el-input>
    </el-form-item>
    <!-- <el-form-item>
      <el-button
        type="primary"
        @click="dataFormSubmit()"
      >确定</el-button>
    </el-form-item> -->
  </el-form>

</template>

<script>
export default {
  props: ['info'],
  data () {
    return {
      dataForm: {
        id: 0,
        type: '1',
        number: '',
        price: '',
        code: '',
        taxAmount: '',
        title: '',
        addressPhone: '',
        idNum: '',
        bankAccount: '',
        userId: '',
        note: '',
        createTime: '',
        state: ''
      },
      typeFlag: false,
      dataRule: {
        type: [
          { required: true, message: '发票类型不能为空', trigger: 'blur' }
        ],
        number: [
          { required: true, message: '发票号码不能为空', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '发票金额不能为空', trigger: 'blur' }
        ],
        userId: [
          { required: true, message: '入账人不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/hotel/thotelfloorroomcheckinpeopleinvoice/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.type = data.tHotelFloorRoomCheckinPeopleInvoice.type
              this.dataForm.number = data.tHotelFloorRoomCheckinPeopleInvoice.number
              this.dataForm.price = data.tHotelFloorRoomCheckinPeopleInvoice.price
              this.dataForm.code = data.tHotelFloorRoomCheckinPeopleInvoice.code
              this.dataForm.taxAmount = data.tHotelFloorRoomCheckinPeopleInvoice.taxAmount
              this.dataForm.title = data.tHotelFloorRoomCheckinPeopleInvoice.title
              this.dataForm.addressPhone = data.tHotelFloorRoomCheckinPeopleInvoice.addressPhone
              this.dataForm.idNum = data.tHotelFloorRoomCheckinPeopleInvoice.idNum
              this.dataForm.bankAccount = data.tHotelFloorRoomCheckinPeopleInvoice.bankAccount
              this.dataForm.userId = data.tHotelFloorRoomCheckinPeopleInvoice.userId
              this.dataForm.note = data.tHotelFloorRoomCheckinPeopleInvoice.note
              this.dataForm.createTime = data.tHotelFloorRoomCheckinPeopleInvoice.createTime
              this.dataForm.state = data.tHotelFloorRoomCheckinPeopleInvoice.state
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/hotel/thotelfloorroomcheckinpeopleinvoice/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'type': this.dataForm.type,
              'number': this.dataForm.number,
              'price': this.dataForm.price,
              'code': this.dataForm.code,
              'taxAmount': this.dataForm.taxAmount,
              'title': this.dataForm.title,
              'addressPhone': this.dataForm.addressPhone,
              'idNum': this.dataForm.idNum,
              'bankAccount': this.dataForm.bankAccount,
              'userId': this.dataForm.userId,
              'note': this.dataForm.note,
              'state': 1
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.$emit('close')
                }
              })
              this.resetFields();

            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    resetFields () {
      this.$refs['dataForm'].resetFields()
      this.typeFlag = false;
    },
    typeChange (val) {
      if (val == '2') {
        this.typeFlag = true;
      } else {
        this.typeFlag = false;
      }
    }
  },
  mounted () {

  }
}
</script>

<style lang="scss" scoped> 
.el-form {
  .el-radio+.el-radio {
    margin-left: 0;
  }
}
</style>
